<?php
namespace Controller;
use Kuyuan\WqCore\wx\KeywordReply;
use Kuyuan\WqCore\controller\Controller;
use Model\W7CoverReplyModel;
use Model\W7ModulesBindingsModel;
use Model\W7RuleKeywordModel;

class Enter extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取模块前端入口列表
     */
    public function getList()
    {
        $_enter = module_entries(KUYUAN_NAME);
        $_data = array();
        foreach($_enter["cover"] as $_k=>$_v){
            $_data[$_k]["url"] = $this->_w['siteroot'].'app/'.ltrim($_v["url"],"./");
            $_data[$_k]["qrcode"] = $this->getQrcode("enter",$_v["do"],$_data[$_k]["url"]);
            $_data[$_k]["name"] = $_v["title"];
            $_keyword = $this->getCoverKeyword($_v["do"]);
            $_data[$_k]["keyword"] = $_keyword ? $_keyword : '';
            $_data[$_k]["do"] = $_v["do"];
        }
        unset($_enter);
        return $_data;
    }

    /**
     * 通过入口页面do参数返回入口页面关键词
     * @param string $_do
     * @return string 返回关键词
     */
    private function getCoverKeyword($_do)
    {
        $_rid = W7CoverReplyModel::where("uniacid",$this->_uniacid)
                           ->where("do",$_do)
                           ->where("module",KUYUAN_NAME)
                           ->field(array("rid"))
                           ->value("rid");
        if($_rid){
            return W7RuleKeywordModel::where("rid",$_rid)
                        ->value("content");
        }
        return false;
    }

    /**
     * 根据一个模块前端入口do参数，获取模块的链接、二维码、入口名称
     * @param string $_do
     * @return array
     */
    public function getLink($_do)
    {
        $_data = array();
        $_data["title"] = W7ModulesBindingsModel::where("do",$_do)
                                    ->where("module",KUYUAN_NAME)
                                    ->where("entry","cover")
                                    ->value("title");
        $_data["url"] = murl("entry//".$_do,array('i'=>$this->_uniacid,'m'=>KUYUAN_NAME),true,true);
        $_data["qrcode"] = $this->getQrcode("entry",$_do,$_data["url"]);
        return $_data;
    }

    /**
     * 设置入口触发关键词
     * @param string $_do 前端入口标识
     * @param string $_name 关键词绑定的名称
     * @param array $_data  数据 _data['keyword'] _data['thumb'] _data['description']
     * @param string $_source_keyword 原关键词
     * @return array|boolean
     */
    public function setKeyword($_do,$_name,$_data,$_source_keyword)
    {
        $_reply = W7CoverReplyModel::where("uniacid",$this->_uniacid)
                                    ->where("module",KUYUAN_NAME)
                                    ->where("do",$_do)
                                    ->field(array("id","rid"))
                                    ->find();
        if($_reply){
            $_update_keyword_result = true;
            if($_source_keyword !== $_data["keyword"]){
                $_update_keyword_result = $this->updateKeyword($_reply["rid"],$_source_keyword,$_data["keyword"]);
                if(!$_update_keyword_result) return false;
            }
            return $this->editKeyword($_reply["id"],$_data) || $_update_keyword_result;
        }else{
            return $this->addKeyword($_do,$_name,$_data);
        }
    }

    /**
     * 修改入口回复规则关键词
     * @param int $_rid  rid
     * @param string $_source_keyword 原关键字
     * @param string $_keyword 修改的关键字
     * @return boolean|array
     */
    private function updateKeyword($_rid,$_source_keyword,$_keyword)
    {
        $KeywordReply = new KeywordReply($_keyword);
        if($KeywordReply->testRepeat()){
            return false;
        }
        return W7RuleKeywordModel::where("rid",$_rid)
                    ->where("module","cover")
                    ->where("content",$_source_keyword)
                    ->update(array("content"=>$_keyword));
    }

    /**
     * 新增入口回复规则
     * @param string $_do 前端入口标识
     * @param string $_name 关键词绑定的名称
     * @param array $_data  数据 _data['keyword'] _data['thumb'] _data['description']
     * @return boolean
     */
    private function addKeyword($_do,$_name,$_data)
    {
        $_keyword = $_data["keyword"];
        $KeywordReply = new KeywordReply($_keyword,$_name);
        if($KeywordReply->testRepeat()){
            return false;
        }
        $_rid = $KeywordReply->add(true);
        $_insert_data = array(
            "uniacid"=>$this->_uniacid,
            "multiid"=>0,
            "rid"=>$_rid,
            "module"=>KUYUAN_NAME,
            "do"=>$_do,
            "title"=>$_name,
            "description"=>$_data["description"],
            "thumb"=>$_data["thumb"],
            "url"=>murl("entry//".$_do,array('i'=>$this->_uniacid,'m'=>KUYUAN_NAME))
        );
        return W7CoverReplyModel::create($_insert_data);
    }

    /**
     * 修改入口回复规则
     * @param $_reply_id 规则id
     * @param array $_data  数据
     * @return boolean
     */
    private function editKeyword($_reply_id,$_data)
    {
        return W7CoverReplyModel::where("id",$_reply_id)
                                ->update(array("description"=>$_data["description"],"thumb"=>$_data["thumb"]));
    }

    /**
     * 通过前端入口标识获取关键词
     * @param $_do
     * @return mixed
     */
    public function getKeyword($_do)
    {
        $_data = W7CoverReplyModel::where("uniacid",$this->_uniacid)
                            ->where("module",KUYUAN_NAME)
                            ->where("do",$_do)
                            ->field(array("description","thumb","rid"))
                            ->find();
        if($_data){
            $_data["keyword"] = W7RuleKeywordModel::where("rid",$_data["rid"])
                        ->where("uniacid",$this->_uniacid)
                        ->value("content");
        }
        return $_data;
    }
}